package com.xian.cloud.config;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Author: xlr
 * @Date: Created in 下午9:36 2019/2/14
 */
@Component
@Qualifier("somethingChannelInitializer")
public class NettyWebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    private TextWebSocketFrameHandler textWebSocketFrameHandler;

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast( new HttpServerCodec() );
        pipeline.addLast( new HttpObjectAggregator( 65536 ) );
        pipeline.addLast( new ChunkedWriteHandler() );
        pipeline.addLast( new WebSocketServerProtocolHandler( "/ws" ) );
        pipeline.addLast( textWebSocketFrameHandler );   //这里不能使用new，不然在handler中不能注入依赖

    }
}
